package day21;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Homework2 {
    static Scanner sc=new Scanner(System.in);
    List<Book> list;
    public Homework2(){
        this.list=new ArrayList<>();
    }
    public void addBook(){
        System.out.print("请输入书名:");
        String bookName=sc.next();
        System.out.print("请输入作者:");
        String author=sc.next();
        System.out.print("请输入价格:");
        Integer price= sc.nextInt();
        list.add(new Book(bookName,author,price));
        System.out.println("书籍"+bookName+"添加成功！");

    }
    public void deleteBook(){
        System.out.print("请输入书名:");
        String bookName=sc.next();
        for (Book book:list){
            if (book.bookName.equals(bookName)){
                list.remove(book);
            }else {
                System.out.println("该书籍不存在！");
            }
        }
        list.removeIf(list -> list.bookName.equals(bookName));
        System.out.println("书籍"+bookName+"删除成功！");
    }
    public void findBook(){
        System.out.print("请输入书名:");
        String bookName=sc.next();
        for (Book book:list){
            if (book.bookName.equals(bookName)){
                System.out.println("书名:"+book.bookName+",作者:"+book.author+",价格:"+book.price);
            }else {
                System.out.println("该书籍不存在！");
            }
        }
    }
    public void update(){
        System.out.print("请输入书名:");
        String bookName=sc.next();
        System.out.print("请输入价格:");
        Integer price=sc.nextInt();
        for (Book book:list){
            if (book.bookName.equals(bookName)){
                book.price=price;
                System.out.println("书籍"+bookName+"价格修改成功！");
            }else {
                System.out.println("该书籍不存在！");
            }
        }
    }
    public void showBook(){
        for (Book book:list){
            System.out.println("书名:"+book.bookName+",作者:"+book.author+",价格:"+book.price);
        }
    }
    public void sortBook(){
        list.sort(((o1, o2) -> {
            int i = o1.price - o2.price;
            return i;
        }));
        for (Book book:list){
            System.out.println("书名:"+book.bookName+",作者:"+book.author+",价格:"+book.price);
        }
    }

    public static void main(String[] args) {
        Homework2 h2=new Homework2();
        while (true){
            System.out.println("***************");
            System.out.println("1.添加图书");
            System.out.println("2.删除图书");
            System.out.println("3.查找图书");
            System.out.println("4.更新图书价格");
            System.out.println("5.显示所有图书");
            System.out.println("6.排序图书");
            System.out.println("7.退出");
            System.out.println("***************");
            System.out.print("请输入对应的编号:");
            int n=sc.nextInt();
            switch (n){
                case 1:h2.addBook();break;
                case 2:h2.deleteBook();break;
                case 3:h2.findBook();break;
                case 4:h2.update();break;
                case 5:h2.showBook();break;
                case 6:h2.sortBook();break;
                case 7:return;
                default:
                    System.out.println("输入错误，请重新输入！");
                    break;
            }
        }
    }
}
class Book{
    String bookName;
    String author;
    Integer price;

    public Book(String bookName, String author, Integer price) {
        this.bookName = bookName;
        this.author = author;
        this.price = price;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}